package com.lanou3g.ssm.web;

import com.lanou3g.ssm.bean.Message;
import com.lanou3g.ssm.bean.ResponseStatus;
import com.lanou3g.ssm.bean.User;
import com.lanou3g.ssm.service.MessageService;
import com.lanou3g.ssm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/message")
public class MessageController {

    @Autowired
    private MessageService messageService;

    @Autowired
    private UserService userService;

    /**
     * 收件箱
     * @param session
     * @param model
     * @return
     */
    @RequestMapping("/to_receive_list")
    // 表示只处理get请求
//    @RequestMapping(value = "/to_main", method = RequestMethod.GET)
    public String toReceiveMessageList(HttpSession session, Model model) {
        List<Message> messageList = messageService.queryReceiveMessage(session);
        model.addAttribute("messageList", messageList);
        return "message/receive_message_list";
    }

    /**
     * 发件箱
     * @param session
     * @param model
     * @return
     */
    @RequestMapping("/to_send_list")
    // 表示只处理get请求
//    @RequestMapping(value = "/to_main", method = RequestMethod.GET)
    public String toSendMessageList(HttpSession session, Model model) {
        List<Message> messageList = messageService.querySendMessage(session);
        model.addAttribute("messageList", messageList);
        return "message/send_message_list";
    }

    /**
     * 去消息发送页
     * @param model 存放需要传到消息发送页的参数(接收人列表)
     * @param session
     * @return
     */
    @RequestMapping("/to_send")
    public String toSendPage(Model model, HttpSession session) {
        List<User> userList = userService.queryAllUserWithoutMe(session);
        model.addAttribute("userList", userList);
        return "message/send";
    }

    /**
     * 发送消息
     * @param message   消息对象
     * @param file  上传的附件
     * @param req
     * @return
     */
    @RequestMapping("/send")
    public String sendMessage(Message message, @RequestParam("myFile") MultipartFile file, HttpServletRequest req) {
        messageService.sendMessage(message, file, req);
        return "forward:/message/to_send_list";
    }

    /**
     * 去往读消息界面
     * @param id    消息ID
     * @param fromPage  从哪个页面点进来的
     *                  (如果从发件箱点进来，则要展示接收人；如果从收件箱点进来，则要展示发送人)
     * @param model 要往读消息页面传的参数
     * @return
     */
    @RequestMapping("/to_read/{id}/{from_page}")
    public String toReadPage(@PathVariable("id") Integer id, @PathVariable("from_page") String fromPage, Model model) {
        messageService.toReadPage(id, fromPage, model);
        return "message/read";
    }

    // 这种纯页面跳转的方法就可以直接通过配置一个视图控制器代替
    /*@RequestMapping("/to_main")
    public String toMainPage() {
        return "main";
    }*/
}
